/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.api.plugin.objectinfo;

import com.farmcode.api.plugin.ObjectInfoPlugin;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author oscar
 */
public class PluginManagerDescriptor implements ObjectInfoPluginManager,Serializable {

    private String name;
    private String version;
    private String fileDescriptor;
    private String simpleName;

    public PluginManagerDescriptor(String fileDescriptor) {
        this.fileDescriptor = fileDescriptor;
    }

    public PluginManagerDescriptor() {
    }

    private Properties getProperties() throws IOException {

        Properties properties = new Properties();
        final ClassLoader cl = Thread.currentThread().getContextClassLoader();
        final InputStream inStream = cl.getResourceAsStream(fileDescriptor);
        if (inStream != null) {
            try {
                properties.load(inStream);
            } finally {
                inStream.close();
            }
        }

        return properties;
    }

    @Override
    public ObjectInfoPlugin getPlugin(String name) throws PluginNotFoundException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the name
     */
    @Override
    public String getName() {
         try {
            name = getProperties().getProperty("name");
        } catch (IOException ex) {
            Logger.getLogger(PluginManagerDescriptor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the version
     */
    @Override
    public String getVersion() {
        try {
            version = getProperties().getProperty("version");
        } catch (IOException ex) {
            Logger.getLogger(PluginManagerDescriptor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return version;
    }

    /**
     * @param version the version to set
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * @return the fileDescriptor
     */
    public String getFileDescriptor() {
        return fileDescriptor;
    }

    /**
     * @param fileDescriptor the fileDescriptor to set
     */
    public void setFileDescriptor(String fileDescriptor) {
        this.fileDescriptor = fileDescriptor;
    }

    /**
     * @return the simpleName
     */
    public String getSimpleName() {
        try {
            simpleName = getProperties().getProperty("simpleName");
        } catch (IOException ex) {
            Logger.getLogger(PluginManagerDescriptor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return simpleName;
    }

    /**
     * @param simpleName the simpleName to set
     */
    public void setSimpleName(String simpleName) {
        this.simpleName = simpleName;
    }
}
